import { Component, OnInit } from '@angular/core';

import { CommonService } from '../../service/common.service'
import { Router } from '@angular/router'
import { EventService } from '../../service/event.service'
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-xchtb',
  templateUrl: './xchtb.page.html',
  styleUrls: ['./xchtb.page.scss'],
})
export class XchtbPage implements OnInit {
  public fil:number=0;
  public money:number;

  public xchadd:string;

  public userInfo:any;
  public flag:boolean = false;
  public password:any;
  public show:boolean=false;
  public pwflag:boolean=false;
  constructor(private common:CommonService,private router:Router,private eventService:EventService,
  private alertController:AlertController) { }

  ngOnInit() {
    this.presentAlertUrl()
    this.listData()
    this.userInfo = this.common.getInfo()
    this.filDetailsOfDay()

    //监听 信息缓存 事件
    this.eventService.event.on('userfile',()=>{
      console.log('更新fil')
      this.filDetailsOfDay()
    })
  }

  // 弹出框
  async presentAlertUrl() {
    const alert = await this.alertController.create({
        // cssClass: 'my-custom-class',
        header: '提示',
        // subHeader: 'Subtitle',
        message: "xch暂未开发！！！！",
        buttons: [
          {
            text: '确定',
            handler: (blah) => {
              this.router.navigateByUrl('tabs/tab3')
            }
          }
        ]
    });
    await alert.present();
  }

  onTransfer(){
    this.money = Math.floor(this.fil) 
  }

  confirm(){

    if(this.money == null || this.money == undefined){
      this.common.presentToast("提币个数不能为空")
      return false
    }

    if(this.money < 1){
      this.common.presentToast("提币不能低于1FIL")
      return false
    }

    if((this.money+'').indexOf(".") !== -1){
      this.common.presentToast("请输入整数")
      return false
    }

    var api = '/tbr/getTestWithdrawadLog'
    var params = {
        userId:this.userInfo.userId,
        fileCoin:this.money,
    }
    
    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == '200'){
        // this.fil = res.data
        this.money = null
        this.event()
        this.common.presentToast("提币成功")
        this.router.navigateByUrl('tabs/tab3')
      }
    })
  }

  // 我的ipfs矿机
  filDetailsOfDay(){
    var api = '/tbr/getFilDetailsOfDay'
    this.common.get(api).then((res:any)=>{
      // console.log(res)
      if(res.code == 200){
        this.fil = res.data.usableFilCoin
      }
    })
  }

  // 我的ipfs矿机
  listData(){
    var api = '/tbr/wallet/validWallet?coinType=xch'
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
          this.xchadd = res.data.xch
      }
    })
  }


  event(){
    // 通知用户中心更新用户信息
    this.eventService.event.emit('userfile');
  }

}
